package com.yuyou.train.lesson.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.lesson.service.IRuleService;
import com.yuyou.train.lesson.vo.req.ReqRuleAddOrUpdate;
import com.yuyou.train.service.api.lesson.dto.RuleDetailDTO;
import com.yuyou.train.service.api.lesson.entity.Rule;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.tool.DefindBeanUtils;

@Validated
@RestController
@RequestMapping(value="/api/rule")
public class RuleController {

	
	@Autowired
	private IRuleService ruleService;
	
	@ApiOperation(value="新增规则")
	@RequestMapping(value="/addOrUpdateRule",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> addOrUpdateRule(@Validated @ModelAttribute ReqRuleAddOrUpdate reqRuleAddOrUpdate){			
		Rule rule=new Rule();	
		DefindBeanUtils.copyProperties(reqRuleAddOrUpdate, rule);	
		ResBoolSimpleInfo<String> res=ruleService.addOrUpdateRule(rule);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="删除规则")
	@ApiImplicitParam(name = "id", value = "规则ID", paramType="query", required = true)
	@RequestMapping(value="/delRule",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> delRule(
			@NotNull(message="id不能为空") String id){		
		ResBoolSimpleInfo<String> res=ruleService.delRule(id);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="规则详情")
	@ApiImplicitParam(name = "id", value = "规则ID", paramType="query", required = true)
	@RequestMapping(value="/detailRule",method=RequestMethod.POST)
	public ReturnResult<RuleDetailDTO> detailRule(
			@NotNull(message="id不能为空")String id){	
		RuleDetailDTO rule=ruleService.selectDetailByPrimaryKey(id);			
		return ReturnResult.success(rule);
	}
	
	@ApiOperation(value="规则分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "isUse", value = "是否启用：0：否，1：是", paramType="query", required = false),
		@ApiImplicitParam(name = "resourceType", value = "资源类别：01：视频，02：音频，03：文件，04：试卷，05：作业（自评作业），06：作业（提交型作业），07：感想", paramType="query",required = false)}
		)
	@RequestMapping(value="/findRulePage",method=RequestMethod.POST)
	public ReturnResult<Pagination<RuleDetailDTO>> findRulePage(
			@Validated @ModelAttribute PageParams pageParams,
			String resourceType,String isUse){				
		Map<String, Object> conditions=new HashMap<String, Object>();
		conditions.put("resourceType", resourceType);
		conditions.put("isUse", isUse);
		Pagination<RuleDetailDTO> queryPgn = new Pagination<RuleDetailDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total=ruleService.findRulePageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<RuleDetailDTO> list=ruleService.findRulePageList(queryPgn);
			queryPgn.setList(list);
		}	
		return ReturnResult.success(queryPgn);
	}

	
	
}
